/**
 * Created with JetBrains WebStorm.
 * User: Chacha
 * Date: 12/06/13
 * Time: 13:40
 * To change this template use File | Settings | File Templates.
 */

var Planter = function(world){

    Action.call(this,world);
    var self = this;

    this.name="Planter";

    this.world = world;





    this.globalImage = new Image();
    this.globalImage.src = "/images/planter_50.png";

    this.cornImage = new Image();
    this.cornImage.src = "/images/mais.png";

    this.carrotImage = new Image();
    this.carrotImage.src = "/images/carottes.png";




    var self = this;
    this.plantCorn = {
        name : "Maïs",
        do : function(tile,worldInterface)
        {
            console.log(World.plants);
            self.world.networkManager.plant(//TODO
                {plantation : World.getFor(World.plants,"name","mais").id,tile : tile.id,
                    maturity: World.getFor(World.maturity,"name","seeded").id,
                    health : World.getFor(World.healths,"name","good").id,real_maturity: 0, real_health: 100},
            function(p){
                World.instance.addPlantation(p);
                World.instance.getPersonage().money -= World.getFor(World.plants,"name","mais").seedPrice;
            });
            worldInterface.dismiss();
        },

        drawButton : function(ctx,worldInterface,actionButton){

            ctx.drawImage(
                self.cornImage,
                actionButton.x, actionButton.y, actionButton.width, actionButton.heigh);
            worldInterface.showFillText(ctx,this.name,
                actionButton.x + actionButton.width/2, actionButton.y - 10,"#7EDAEF","15pt Calibri","","center");
        }
    }
    this.plantCarrot = {
        name : "Carotte",
        do : function(tile,worldInterface)
        {
            alert("Sorry no yet dev.");
            worldInterface.dismiss();
        },

        drawButton : function(ctx,worldInterface,actionButton){

            ctx.drawImage(
                self.carrotImage,
                actionButton.x, actionButton.y, actionButton.width, actionButton.heigh);
            worldInterface.showFillText(ctx,this.name,
                actionButton.x + actionButton.width/2, actionButton.y - 10,"#7EDAEF","15pt Calibri","","center");
        }
    }



}

Planter.prototype = {

    canDo : function(tile,personage){

        if (this.distanceReq(tile,personage,300)){
            if(this.checkOwnership(tile,personage)){
                if(this.checkFreeTile(tile)){

                        return true;
                }
            }

        }
        return false;
    },

    getFor : function(name){
        for(var i in this.plants){
            if (this.plants[i].name == name)
                return this.plants[i];
        }
    },

    getMFor : function(name){
        for(var i in this.maturity){
            if (this.maturity[i].name == name)
                return this.maturity[i];
        }
    },

    do : function(tile,worldInterface){

        worldInterface.save();

        worldInterface.show(tile,[

            this.plantCorn,
            this.plantCarrot

        ],true);


    },
    //To draw the button as you wish
    drawButton : function(ctx,worldInterface,actionButton){

        ctx.drawImage(
            this.globalImage,
            actionButton.x, actionButton.y, actionButton.width, actionButton.heigh);
        worldInterface.showFillText(ctx,this.name,
            actionButton.x + actionButton.width/2, actionButton.y - 10,"#7EDAEF","15pt Calibri","","center");
    }



}